<template>
  <el-form-item label="标签">
    <el-input v-model="currentItem.label" />
  </el-form-item>
  <el-form-item label="是否必选">
    <el-radio-group v-model="elFormItemProps.required">
      <el-radio-button label="true" :value="true" />
      <el-radio-button label="false" :value="false" />
    </el-radio-group>
  </el-form-item>
  <el-form-item label="是否可筛选">
    <el-radio-group v-model="componentProps.filterable">
      <el-radio-button :value="false">不允许</el-radio-button>
      <el-radio-button :value="true">允许</el-radio-button>
    </el-radio-group>
  </el-form-item>
  <el-button class="mt-4" style="width: 100%" @click="onAddItem">
    新增
  </el-button>
  <el-form-item label="是否多选">
    <el-radio-group v-model="componentProps.multiple" @change="clearData">
      <el-radio-button :value="false">单选</el-radio-button>
      <el-radio-button :value="true">多选</el-radio-button>
    </el-radio-group>
  </el-form-item>
  <template v-if="componentProps.multiple">
    <!-- 多选 -->
    <el-form-item label="最多项目数,0不限制">
      <el-input-number v-model="componentProps.multipleLimit" />
    </el-form-item>
    <el-form-item label="将选中值按文字的形式展示">
      <el-radio-group v-model="componentProps.collapseTags">
        <el-radio-button :value="false">不允许</el-radio-button>
        <el-radio-button :value="true">允许</el-radio-button>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="鼠标悬停展示">
      <el-radio-group v-model="componentProps.collapseTagsTooltip">
        <el-radio-button :value="false">不允许</el-radio-button>
        <el-radio-button :value="true">允许</el-radio-button>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="显示的 Tag 的最大数量">
      <el-input-number v-model="componentProps.collapseTagsTooltip" />
    </el-form-item>
  </template>
  <el-form-item label="允许创建新条目">
    <el-radio-group v-model="componentProps.allowCreate">
      <el-radio-button :value="false">不允许</el-radio-button>
      <el-radio-button :value="true">允许</el-radio-button>
    </el-radio-group>
  </el-form-item>
  <el-table :data="componentData.options" style="width: 100%" max-height="500">
    <el-table-column fixed prop="label" label="label">
      <template #default="scope">
        <el-input v-model="scope.row.label" />
      </template>
    </el-table-column>
    <el-table-column prop="value" label="value">
      <template #default="scope">
        <el-input v-model="scope.row.value" />
      </template>
    </el-table-column>
    <el-table-column fixed="right" label="Operations">
      <template #default="scope">
        <el-button
          link
          type="primary"
          size="small"
          @click.prevent="deleteRow(scope.$index)"
        >
          移除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script setup lang="ts">
import { computed, PropType } from "vue";
import { DraggableTreeNode } from "../../../type";
import { SelectComponentData, SelectComponentProps } from "./display-select";
const props = defineProps({
  currentItem: {
    type: Object as PropType<
      DraggableTreeNode<SelectComponentProps, SelectComponentData>
    >,
    default: () => {
      return {};
    },
  },
});
const componentProps = computed({
  get() {
    return (
      props.currentItem?.component?.componentProps || {
        multiple: false,
      }
    );
  },
  set(value) {
    if (props.currentItem?.component) {
      props.currentItem.component.componentProps = value;
    }
  },
});
const emit = defineEmits(["update:currentItem"]);
const currentItem = computed({
  get() {
    return (
      props.currentItem || {
        label: "",
        component: {},
      }
    );
  },
  set(value) {
    emit("update:currentItem", value);
  },
});

const componentData = computed({
  get() {
    return (
      props.currentItem?.component?.componentData || {
        options: [],
      }
    );
  },
  set(value) {
    if (props.currentItem?.component) {
      props.currentItem.component.componentData = value;
    }
  },
});
const deleteRow = (index: number) => {
  componentData.value.options.splice(index, 1);
};

const onAddItem = () => {
  componentData.value.options.push({
    label: `label${componentData.value.options.length}`,
    value: `value${componentData.value.options.length}`,
  });
};
const clearData = (val: boolean) => {
  if (val) {
    componentProps.value.modelValue = [];
  } else {
    componentProps.value.modelValue = "";
  }
};
const elFormItemProps = computed({
  get() {
    return props.currentItem?.component?.elFormItemProps || {};
  },
  set(value) {
    if (props.currentItem?.component) {
      props.currentItem.component.elFormItemProps = value;
    }
  },
});
</script>

<style lang="scss" scoped></style>
